package io.renren.modules.pms.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.modules.pms.entity.dto.ProductConditionDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.pms.entity.ProductEntity;
import io.renren.modules.pms.service.ProductService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.websocket.server.PathParam;


/**
 * 商品信息
 *
 * @author LuoShun
 * @email lori6166@163.com
 * @date 2021-11-03 12:13:00
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 分页获取商品列表
     */
    @ApiOperation("分页条件查询获取商品列表")
    @GetMapping("/list")
    @RequiresPermissions("pms:product:list")
    public R list(ProductConditionDTO condition){
        Page page = productService.list(condition);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("pms:product:info")
    public R info(@PathVariable("id") Long id){
		ProductEntity product = productService.getById(id);

        return R.ok().put("product", product);
    }

    /**
     * 添加商品
     */
    @ApiOperation("添加商品")
    @PostMapping("/create")
    @RequiresPermissions("pms:product:save")
    public R save(@RequestBody ProductEntity product){
		productService.save(product);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("pms:product:update")
    public R update(@RequestBody ProductEntity product){
		productService.updateById(product);

        return R.ok();
    }

    /**
     * 删除(修改删除状态)
     */
    @ApiOperation("删除(修改删除状态)")
    @PostMapping("/update/deleteStatus")
    @RequiresPermissions("pms:product:update:updateDeleteStatus")
    public R delete(@PathParam("ids") Long[] ids,
                    @PathParam("deleteStatus") Integer deleteStatus){
        productService.updateDeleteStatus(ids,deleteStatus);

        return R.ok();
    }

    /**
     * 上下架商品
     */
    @ApiOperation("上下架商品")
    @PostMapping("/update/publishStatus")
    @RequiresPermissions("pms:product:update:updatePublishStatus")
    public R updatePublishStatus(@PathParam("ids") Long[] ids,
                    @PathParam("publishStatus") Integer publishStatus){
        productService.updatePublishStatus(ids,publishStatus);

        return R.ok();
    }

    /**
     * 设定商品是否推荐
     */
    @ApiOperation("设定商品是否推荐")
    @PostMapping("/update/recommendStatus")
    @RequiresPermissions("pms:product:update:updateRecommendStatus")
    public R updateRecommendStatus(@PathParam("ids") Long[] ids,
                                 @PathParam("recommendStatus") Integer recommendStatus){
        productService.updateRecommendStatus(ids,recommendStatus);

        return R.ok();
    }

    /**
     * 设定商品是否新品
     */
    @ApiOperation("设定商品是否新品")
    @PostMapping("/update/newStatus")
    @RequiresPermissions("pms:product:updateNewStatus")
    public R updateNewStatus(@PathParam("ids") Long[] ids,
                                   @PathParam("newStatus") Integer newStatus){
        productService.updateNewStatus(ids,newStatus);

        return R.ok();
    }

}
